package com.hbpu.encap.generic;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Comparator;

public class Exercise01 {
    public static void main(String[] args) {
        Employee jack = new Employee("jack", 2500, new MyDate(2002, 9, 28));
        Employee bob = new Employee("bob", 4000, new MyDate(2000, 12, 15));
        Employee mike = new Employee("jack", 5000, new MyDate(1997, 6, 7));

        ArrayList<Employee> employees = new ArrayList<>();

        employees.add(jack);
        employees.add(bob);
        employees.add(mike);

        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                if (!(o1.getName().equals(o2.getName()))) {
                    return o1.getName().compareTo(o2.getName());
                } else {
                    return o1.getBirthday().compareTo(o2.getBirthday());
                }

            }
        });

        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

}

class Employee {
    private String name;
    private double sal;
    private MyDate birthday;

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday.toString() +
                '}';
    }
}

class MyDate implements Comparable<MyDate> {
    private int year;
    private int month;
    private int day;


    public MyDate(int year, int month, int day) {
        this.month = month;
        this.day = day;
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    @Override
    public int compareTo(@NotNull MyDate o) {
        int yearDiff = this.year - o.year;
        if (yearDiff != 0) {
            return yearDiff;
        }

        int monthDIff = this.month - o.month;
        if (monthDIff != 0) {
            return monthDIff;
        }

        return this.day - o.day;
    }
}